package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Menu;
import com.itheima.service.MemberService;
import com.itheima.service.MenusService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = MenusService.class)
@Transactional
public class MenuServiceImpl implements MenusService {
    @Autowired
    private MenuDao menuDao;
    @Override
    public List<Menu> findMenusByUsername(String name) {
        //通过递归,查询不确定层级的菜单
        //首先查询出一级菜单
        List<Menu> menuList = menuDao.getLevelOneMenuByUserName(name);
        return getMenuById(menuList);
    }

    public List<Menu> getMenuById(List<Menu> menuList){
        for (int i = 0; i < menuList.size(); i++) {
            Integer id = menuList.get(i).getId();
            List<Menu> menuListSon = menuDao.getMenuById(id);
            if (menuListSon != null && menuListSon.size() > 0) {
                menuList.get(i).setChildren(menuListSon);
                getMenuById(menuListSon);
            } else {
                //System.out.println(menuList);
                return menuList;
            }

        }
        //System.out.println(menuList);
        return menuList;
    }

}
